﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ImageK.Process;

namespace ImageK.Gui
{
    /** This is a virtual stack of frozen plots. */
    public class PlotVirtualStack : VirtualStack
    {

        private ArrayList plots = new ArrayList(50);
        private int bitDepth = 8;

        public PlotVirtualStack(int width, int height): base(width, height)
        {
            this.bitDepth = bitDepth;
        }

        /** Adds a plot to the end of the stack. */
        public void addPlot(Plot plot)
        {
            plots.Add(plot.toByteArray());
            if (plot.isColored())
                bitDepth = 24;
        }

        /** Returns the pixel array for the specified slice, where {@literal 1<=n<=nslices}. */
        public Object getPixels(int n)
        {
            ImageProcessor ip = getProcessor(n);
            if (ip!=null)
                return ip.getPixels();
            else
                return null;
        }

        /** Returns an ImageProcessor for the specified slice,
		    where {@literal 1<=n<=nslices}. Returns null if the stack is empty. */
        public ImageProcessor getProcessor(int n)
        {
            byte[] bytes = (byte[])plots[n-1];
            if (bytes!=null)
            {
                try
                {
                    Plot plot = new Plot(null, new MemoryStream(bytes));
                    ImageProcessor ip = plot.getProcessor();
                    if (bitDepth==24)
                        ip = ip.convertToRGB();
                    else if (bitDepth==8)
                        ip =  ip.convertToByte(false);
                    return ip;
                }
                catch (Exception e)
                {
                    IJ.handleException(e);
                }
            }
            return null;
        }

        /** Returns the number of slices in this stack. */
        public int getSize()
        {
            return plots.Count;
        }

        /** Returns either 24 (RGB) or 8 (grayscale). */
        public int getBitDepth()
        {
            return bitDepth;
        }

        public void setBitDepth(int bitDepth)
        {
            this.bitDepth = bitDepth;
        }

        public String getSliceLabel(int n)
        {
            return null;
        }

        public void setPixels(Object pixels, int n)
        {
        }

        /** Deletes the specified slice, where {@literal 1<=n<=nslices}. */
        public void deleteSlice(int n)
        {
            if (n<1 || n>plots.Count)
                throw new ArgumentException("Argument out of range: "+n);
            if (plots.Count<1)
                return;
            plots.Remove(n-1);
        }


    } // PlotVirtualStack
}
